package vn.hanelsoft.newsduet.control;

import vn.hanelsoft.newsduet.R;
import vn.hanelsoft.newsduet.model.delegate.ServiceDelegate;
import vn.hanelsoft.newsduet.model.helper.DBHandler;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HomeActivity extends Activity implements OnClickListener {

	// Menu
	private DrawerLayout mDrawerLayout;
	private LinearLayout mDrawerMenu;
	private ActionBarDrawerToggle mDrawerToggle;
	public static final String ARG_PLANET_NUMBER = "planet_number";
	private CharSequence mDrawerTitle;
	private CharSequence mTitle;
	private String[] mPlanetTitles;
	int actionBarTitleId;
	TextView title_bar;
	private final int HighLight = 0, India = 1, WorldWide = 2, Business = 3,
			HealthTopic = 4, LatestSport = 5, ScienceTopic = 6, TechTopic = 7,
			Favourite = 8;
	int[] id_array = { R.id.menu_highLights, R.id.menu_india,
			R.id.menu_woldWide, R.id.menu_business, R.id.menu_healthTopic,
			R.id.menu_latestSport, R.id.menu_scienceTopic, R.id.menu_techTopic,
			R.id.menu_favourite };
	Context mContext;

	// ________________________ System Function

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		mContext = this;
		DBHandler.initdb(mContext);
		initWidget(savedInstanceState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.home, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.menu_highLights:
			selectItem(HighLight, R.id.menu_highLights, true);
			break;
		case R.id.menu_india:
			selectItem(India, R.id.menu_india, true);
			break;
		case R.id.menu_woldWide:
			selectItem(WorldWide, R.id.menu_woldWide, true);
			break;
		case R.id.menu_business:
			selectItem(Business, R.id.menu_business, true);
			break;
		case R.id.menu_healthTopic:
			selectItem(HealthTopic, R.id.menu_healthTopic, true);
			break;
		case R.id.menu_latestSport:
			selectItem(LatestSport, R.id.menu_latestSport, true);
			break;
		case R.id.menu_scienceTopic:
			selectItem(ScienceTopic, R.id.menu_scienceTopic, true);
			break;
		case R.id.menu_techTopic:
			selectItem(TechTopic, R.id.menu_techTopic, true);
			break;
		case R.id.menu_favourite:
			selectItem(Favourite, R.id.menu_favourite, true);
			break;
		default:
			break;
		}

	}

	// ____________________________ Custom Function
	private void initWidget(Bundle savedInstanceState) {
		actionBarTitleId = Resources.getSystem().getIdentifier(
				"action_bar_title", "id", "android");
		title_bar = (TextView) findViewById(actionBarTitleId);
		findViewById(R.id.menu_highLights)
				.setOnClickListener(HomeActivity.this);
		findViewById(R.id.menu_india).setOnClickListener(HomeActivity.this);
		findViewById(R.id.menu_woldWide).setOnClickListener(HomeActivity.this);
		findViewById(R.id.menu_business).setOnClickListener(HomeActivity.this);
		findViewById(R.id.menu_healthTopic).setOnClickListener(
				HomeActivity.this);
		findViewById(R.id.menu_latestSport).setOnClickListener(
				HomeActivity.this);
		findViewById(R.id.menu_scienceTopic).setOnClickListener(
				HomeActivity.this);
		findViewById(R.id.menu_techTopic).setOnClickListener(HomeActivity.this);
		findViewById(R.id.menu_favourite).setOnClickListener(HomeActivity.this);

		mTitle = mDrawerTitle = getTitle();
		mPlanetTitles = getResources().getStringArray(R.array.planets_array);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerMenu = (LinearLayout) findViewById(R.id.left_drawerMenu);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setIcon(android.R.color.transparent);
		getActionBar()
				.setBackgroundDrawable(
						new ColorDrawable(getResources().getColor(
								R.color.bg_actionbar)));
		setText(mTitle.toString());
		mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
		mDrawerLayout, /* DrawerLayout object */
		R.drawable.menu_icon, /* nav drawer image to replace 'Up' caret */
		R.string.app_name, /* "open drawer" description for accessibility */
		R.string.app_name /* "close drawer" description for accessibility */
		) {
			public void onDrawerClosed(View view) {
				setText(mTitle.toString());
			}

			public void onDrawerOpened(View drawerView) {
				setText(mDrawerTitle.toString());
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);
		if (savedInstanceState == null) {
			selectItem(0, R.id.menu_highLights, true);
		}
	}

	private void setText(String str) {
		// if (actionBarTitleId > 0
		// && str.equals(getResources().getString(R.string.app_name))) {
		// if (title_bar != null) {
		// str = " " + str + " ";
		// // title_bar.setTypeface(tf);
		// title_bar.setTextSize(20f);
		// getActionBar().setTitle(str);
		// }
		// } else {
		// str = " " + str + " ";
		// // title_bar.setTypeface(Typeface.DEFAULT);
		// }
		// getActionBar().setTitle(str);
		// invalidateOptionsMenu();
	}

	private void selectItem(int position, int id, boolean FLAG) {
		if (FLAG) {
			// fragment
			Fragment fragment = null;
			switch (position) {
			case HighLight:
				fragment = new HighLightFragment(this, null, true);
				break;
			case India:
				fragment = new HighLightFragment(this, ServiceDelegate.LINK_CATEGORY_INDIA, false);
//				fragment = new IndiaFragment(this, ServiceDelegate.LINK_CATEGORY_INDIA);
				break;
			case WorldWide:
				fragment = new HighLightFragment(this, ServiceDelegate.LINK_CATEGORY_WORLD, false);
//				fragment = new IndiaFragment(this, ServiceDelegate.LINK_CATEGORY_WORLD);
				break;
			case Business:
				fragment = new HighLightFragment(this, ServiceDelegate.LINK_CATEGORY_BUSINESS, false);
//				fragment = new IndiaFragment(this, ServiceDelegate.LINK_CATEGORY_BUSINESS);
				break;
			case HealthTopic:
				fragment = new HighLightFragment(this, ServiceDelegate.LINK_CATEGORY_HEALTH, false);
//				fragment = new IndiaFragment(this, ServiceDelegate.LINK_CATEGORY_HEALTH);
				break;
			case LatestSport:
				fragment = new HighLightFragment(this, ServiceDelegate.LINK_CATEGORY_SPORT, false);
//				fragment = new IndiaFragment(this, ServiceDelegate.LINK_CATEGORY_SPORT);
				break;
			case ScienceTopic:
				fragment = new HighLightFragment(this, ServiceDelegate.LINK_CATEGORY_SCIENCE, false);
//				fragment = new IndiaFragment(this, ServiceDelegate.LINK_CATEGORY_SCIENCE);
				break;
			case TechTopic:
				fragment = new HighLightFragment(this, ServiceDelegate.LINK_CATEGORY_TECHNOLOGY, false);
//				fragment = new IndiaFragment(this, ServiceDelegate.LINK_CATEGORY_TECHNOLOGY);
				break;
			case Favourite:
				fragment = new FavouriteFragment(this);
//				fragment = new IndiaFragment(this, ServiceDelegate.LINK_CATEGORY_TECHNOLOGY);
				break;
			default:
				fragment = new HighLightFragment(this, null, true);
				break;
			}

			Bundle args = new Bundle();
			args.putInt(ARG_PLANET_NUMBER, position);
			fragment.setArguments(args);

			FragmentManager fragmentManager = getFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame, fragment).commit();
			setTitle(mPlanetTitles[position]); // settitle
			mDrawerLayout.closeDrawer(mDrawerMenu);
		} else {
			// activity
			switch (position) {

			default:
				break;
			}
		}

	}

//	private void startActivity(Class<?> activity, String title) {
//		mDrawerLayout.closeDrawer(mDrawerMenu);
//		Intent ie = new Intent(HomeActivity.this, activity);
//		ie.putExtra(KEY_TITLE, title);
//		startActivity(ie);
//	}
}
